package com.java.zhuiyun.api.idiomAdventureManager.service.impl;

import com.java.zhuiyun.api.idiomAdventureManager.entity.IdiomAdventureQuestionBank;
import com.java.zhuiyun.api.idiomAdventureManager.mapper.IdiomAdventureQuestionBankMapper;
import com.java.zhuiyun.api.idiomAdventureManager.model.IdiomAdventureQuestionBankModel;
import com.java.zhuiyun.api.idiomAdventureManager.service.IdiomAdventureQuestionBankService;
import com.java.zhuiyun.common.errorCode.CommonException;
import com.java.zhuiyun.common.errorCode.EmErrorCode;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.BeanUtils;
import com.java.zhuiyun.util.ObjectUtils;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import com.java.zhuiyun.util.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName IdiomAdventureQuestionBankServiceImpl
 * @Description TODO 冒险题库管理服务类 用于实现词库的增删改查
 * @Author WX
 * @Date 2023/9/13 9:20
 **/
@Service

public class IdiomAdventureQuestionBankServiceImpl implements IdiomAdventureQuestionBankService {
    @Autowired
    private IdiomAdventureQuestionBankMapper idiomAdventureQuestionBankMapper;

    /*
     * @Author WX
     * @Description 模糊查询
     * @Date 2023/9/13 10:26
     * @Return
     * @param PageRequest idiomAdventureValue
     */
    @Override
    public CommonResult selectIdiomAdventureQuestionBankAllByOne(IdiomAdventureQuestionBank idiomAdventureQuestionBank, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        List<IdiomAdventureQuestionBankModel> backAdventureModels = idiomAdventureQuestionBankMapper.selectIdiomAdventureQuestionBankAllByOne(idiomAdventureQuestionBank);
        PageResult pageResult = MybatisPageHelper.getPageResult(backAdventureModels);
        return CommonResult.ok(pageResult);
    }

    /*
     * @Author WX
     * @Description 添加词库
     * @Date 2023/9/13 11:18
     * @Return
     * @param idiomAdventureQuestionBank
     */
    @Override
    public CommonResult addIdiomAdventureQuestionBank(IdiomAdventureQuestionBankModel idiomAdventureQuestionBankModel)   throws CommonException{
        if (ValidatorUtil.ValidationObject(idiomAdventureQuestionBankModel)){
            IdiomAdventureQuestionBank idiomAdventureQuestionBank = new IdiomAdventureQuestionBank();
            BeanUtils.copy(idiomAdventureQuestionBankModel,idiomAdventureQuestionBank);
            idiomAdventureQuestionBank.setCreateTime(new Date());
            idiomAdventureQuestionBank.setUpdateTime(new Date());
            idiomAdventureQuestionBank.setDelFlag(0);
            int i = idiomAdventureQuestionBankMapper.addIdiomAdventureQuestionBank(idiomAdventureQuestionBank);
            if (i > 0) {
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            }else {
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        }else {
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }

    /*
     * @Author WX
     * @Description 查询单个
     * @Date 2023/9/13 14:01
     * @Return
     * @param id
     */
    @Override
    public CommonResult findIdiomAdventureQuestionBankById(Integer id) throws CommonException {
        if (ObjectUtils.isNotEmpty(id)){
            IdiomAdventureQuestionBank idiomAdventureQuestionBank = idiomAdventureQuestionBankMapper.findIdiomAdventureQuestionBankById(id);
            if (idiomAdventureQuestionBank != null){
                return CommonResult.ok(idiomAdventureQuestionBank);
            }else {
                return CommonResult.ok(EmErrorCode.ID_NOT_OBJECT);
            }
        }else {
            return CommonResult.ok(EmErrorCode.PARAMETER_VALIDATION_ERROR);
        }
    }

    /*
     * @Author WX
     * @Description 修改单个
     * @Date 2023/9/13 14:07
     * @Return
     * @param idiomAdventureQuestionBank
     */
    @Override
    public CommonResult updateIdiomAdventureQuestionBankById(IdiomAdventureQuestionBankModel idiomAdventureQuestionBankModel) throws CommonException{
        if (ValidatorUtil.ValidationObject(idiomAdventureQuestionBankModel)){
            IdiomAdventureQuestionBank idiomAdventureQuestionBank = new IdiomAdventureQuestionBank();
            BeanUtils.copy(idiomAdventureQuestionBankModel,idiomAdventureQuestionBank);
            idiomAdventureQuestionBank.setUpdateTime(new Date());
            int i = idiomAdventureQuestionBankMapper.updateIdiomAdventureQuestionBankById(idiomAdventureQuestionBank);
            if (i > 0) {
                return CommonResult.ok(EmErrorCode.UPDATE_OK);
            }else {
                return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
            }
        }else {
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }

    /*
     * @Author WX
     * @Description 删除
     * @Date 2023/9/13 14:17
     * @Return
     * @param id
     */
    @Override
    public CommonResult deleteIdiomAdventureQuestionBankById(Integer id) throws CommonException {
        if (ObjectUtils.isNotEmpty(id)){
            int i = idiomAdventureQuestionBankMapper.deleteIdiomAdventureQuestionBankById(id);
            if (i > 0){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else {
                return CommonResult.ok(EmErrorCode.DELETE_ERROR);
            }
        }else {
            return CommonResult.ok(EmErrorCode.ID_NULL);
        }
    }

    /*
     * @Author WX
     * @Description 数据库随机查10条
     * @Date 2023/9/14 13:56
     * @Return
     * @param null
     */
    @Override
    public List<IdiomAdventureQuestionBank>  selectIdiomAdventureQuestionBankRadomByTen() {
        return idiomAdventureQuestionBankMapper.selectIdiomAdventureQuestionBankRadomByTen();
    }


}
